<?php
class AdminServer {
	
	public function pagingBySearch($where="",$order="",$numberEachPage = 10){
		$count = HttpDaoManager::getAdminDao()->count($where);
		$page = new Page($count, $numberEachPage);
		$limit[0]= $page->which;
		$limit[1]=$numberEachPage;
		$list = HttpDaoManager::getAdminDao()->searchList($where,$order,$limit);
		$page1= $page->returnPage("?".ClassManager::getToos()->combinationGet('page')."&page=");
		
		$employee_id_list = Common::array_value_list($list,'employee_id');
		$data['employee_pairs']  = array();
		if(!empty($employee_id_list)){
			$employe_list = HttpDaoManager::getEmployeesDao()->getDataPairList($employee_id_list);
			$data['employee_pairs'] =Common::fetchPairOfList('id', 'name', $employe_list);		
			unset($employe_list);
		}

		$teacher_pairs = array();
		$teacher_id_list = Common::array_value_list($list,'teacher_id');
		if(!empty($teacher_id_list)){
			$teacher_list = HttpDaoManager::getTeacherDao()->getDataPairList($teacher_id_list);
			foreach($teacher_list as $k=>$v){
				$teacher_pairs[$v['id']]=$v['serial_number'].'-'.$v['name'];
			}
			unset($teacher_list);
		}

		$data['teacher_pairs'] =$teacher_pairs;

		$funduser_pairs = array();
		$fund_user_id_list = Common::array_value_list($list,'fund_user_id');
		if(!empty($fund_user_id_list)){
			$funduser_list =  HttpDaoManager::getFunduserDao()->getDataPairList($fund_user_id_list);
			foreach($funduser_list as $key=>$value){
				$funduser_pairs[$value['id']] = $value['serial_number'].'-'.$value['name'];
			}
			unset($funduser_list);
		}
		$data['funduser_pairs'] = $funduser_pairs;
		
		$data['list'] =$list;
		$data['page'] = $page1;
		return $data;
	}
	public function logion($name,$password)	{
		$adminDao = HttpDaoManager::getAdminDao();
		$data = $adminDao->getByNameByPassword($name, $password);
		
		if (empty($data)){
			$bool  =false;
		}
		else{
			$adminGroupDao = HttpDaoManager::getAdminGroupDao();
			$group = $adminGroupDao->get($data['eo_admin_group']);
			$data['group'] = $group;
			$data['admin'] = $group['admin'];
			$_SESSION['admin'] = $data;
			$bool = true;
			//登录日志
			HttpServerManager::getAdminLoginLogServer()->add($name,$data['id']);
		}
		
		return $bool;
	}
	public function quit(){
		unset($_SESSION['admin']);
	}
	/**
	 * 权限控制通过标识
	 * @return boolean 符合权限为真
	 */
	public function permissionsByMark($mark){
		if ($_SESSION['admin']['group']['admin']!=1){
			$bool= false;
			$functionData = HttpDaoManager::getFunctionDao()->getByMark($mark);
			if (!empty($functionData)){
				$bool =   $this->searchPermissions($functionData);
			}
			else{
				$bool = true;
			}
		}
		else{
			$bool = true;
		}
		return $bool;
	}
	/**
	 * 权限控制
	 * @return boolean 符合权限为真
	 */
	public function permissions($controller , $function ){
		if ($_SESSION['admin']['group']['admin']!=1){
			$bool= false;
			//管理员公共权限
			$publicFunction = array('admin_login','admin_maintop','admin_leftjs','admin_home','admin_left','admin_main','admin_top','admin_quit','admin_updatepassword');
			if (in_array(strtolower($controller).'_'.strtolower($function),$publicFunction)){
				$bool = true;
			}
			else{
				$functionData = HttpDaoManager::getFunctionDao()->getByCnByFu($controller, $function);
				if (!empty($functionData)){
					$bool =   $this->searchPermissions($functionData);
				}
				else{
					$bool = true;
				}
			}
		}
		else{
			$bool = true;
		}
		return $bool;
	}
	/**
	 *查看用户是否符合该权限 
	 */
	public function searchPermissions($functionData){
		if ($_SESSION['admin']['group']['admin']==1){
			return true;
		}
		$permissionsFunctoin = explode(',',$_SESSION['admin']['group']['permissions_functoin']);
		if (in_array($functionData['id'], $permissionsFunctoin)){
			return true;			
		}
		else{
			if ($functionData['father_id']!=0){
				$functionData1 = HttpDaoManager::getFunctionDao()->getListByFatherIdNoId($functionData['father_id'],$functionData['id']);
				$i=0;
				foreach ($functionData1 as $v){
					if (in_array($v['id'],$permissionsFunctoin)){
						$i++;
					}
				}
				if ($i>0){
					return false;
				}else{
					$functionData = HttpDaoManager::getFunctionDao()->get($functionData['father_id']);
					return  $this->searchPermissions($functionData);
				}
			}
		}
	}
	public function deleteBatchByid($id){
		if (is_array($id)){
			$id = implode(',',$id);
		}
		HttpDaoManager::getAdminDao()->deleteBatchByid($id);
	}
	public function add($data){
		if ($data['admin_type'] == ADMIN_TYPE_TEACHER){
			if ($data['teacher_id']==DEFAULT_SELECT){
				return ERROR_CODE_ONE;
			}
			$data['dept_id'] = 99 ;
			$data['fund_user_id'] = 0 ;
			$data['employee_id'] = 0 ;
		}
		
		if ($data['admin_type'] == ADMIN_TYPE_EMPLOYEE){
			if ($data['dept_id']==DEFAULT_SELECT||$data['employee_id']==DEFAULT_SELECT){
				return ERROR_CODE_TWO;
			}			
			$data['fund_user_id'] = 0 ;
			$data['teacher_id'] = 0 ;
		}
		
		if ($data['admin_type'] == ADMIN_TYPE_FUNDUSER){
			if ($data['fund_user_id']==DEFAULT_SELECT){
				return ERROR_CODE_TWO;
			}			
			$data['teacher_id'] = 0 ;
			$data['employee_id'] = 0 ;
			$data['dept_id'] = 98 ;
		}
		
		$admindata = HttpDaoManager::getAdminDao()->getByname($data['name']);
		if (!empty($admindata)){
			return ERROR_CODE_THREE;
		}
		else{
			$data['password'] = md5($data['password']);
			HttpDaoManager::getAdminDao()->add($data);
			return true;
		}
	}
	public function update($data,$id){
		if ($data['admin_type'] == ADMIN_TYPE_TEACHER){
			if ($data['teacher_id']==DEFAULT_SELECT){
				return ERROR_CODE_ONE;
			}
			$data['dept_id'] = 99 ;
			$data['fund_user_id'] = 0 ;
			$data['employee_id'] = 0 ;
		}
		
		if ($data['admin_type'] == ADMIN_TYPE_EMPLOYEE){
			if ($data['dept_id']==DEFAULT_SELECT||$data['employee_id']==DEFAULT_SELECT){
				return ERROR_CODE_TWO;
			}			
			$data['fund_user_id'] = 0 ;
			$data['teacher_id'] = 0 ;
		}
		
		if ($data['admin_type'] == ADMIN_TYPE_FUNDUSER){
			if ($data['fund_user_id']==DEFAULT_SELECT){
				return ERROR_CODE_TWO;
			}			
			$data['teacher_id'] = 0 ;
			$data['employee_id'] = 0 ;
			$data['dept_id'] = 98 ;
		}
		
		
		if (empty($data['password'])){
			unset($data['password']);
		}
		else{
			$data['password'] = md5($data['password']);
		}
		HttpDaoManager::getAdminDao()->update($data, $id);
		
	}
	public function updateStatus($EmployeeId){
		$data['status'] =2;
		HttpDaoManager::getAdminDao()->updateByEmployeeId($data, $EmployeeId);
	}
	public function updateStatusByTeacherId($teacherId){
		$data['status'] =2;
		HttpDaoManager::getAdminDao()->updateByTeacherId($data, $teacherId);
	}
	public function updatePassword($password){
		if (empty($password)){
			return 2;
		}
		
		HttpDaoManager::getAdminDao()->updatePassword(md5($password), $_SESSION['admin']['id']);
		return  true;
	}
}
?>